package com.itelite.security;

import com.itelite.common.info.Result;
import com.itelite.common.util.SecurityUtils;
import com.itelite.common.util.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 沈洋 邮箱:1845973183@qq.com
 * @create 2021/8/11-15:39
 **/
@Slf4j
public class MyAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        log.warn("警告： {} | 操作者：{}","越权访问接口-"+request.getRequestURI(),SecurityUtils.getRealName());
        SpringUtils.makeResponse(response,Result.failure(HttpStatus.FORBIDDEN,"访问权限不足"));
    }
}
